package web.blogsphere.model;

import lombok.Data;
import web.blogsphere.enums.ResultStatus;

/**
 * @ClassName Result
 * @Description 统一功能结果返回
 * @Author ZJX
 * @Date 2024/9/3 13:05
 * @Version 1.0
 */
@Data
public class Result<T> {
//    定义错误码
    private ResultStatus code;

    //错误信息
    private String errMsg;

    //接口响应的数据
    private T data;

    public static <T> Result<T> success(T data){
        Result<T> result = new Result<>();
        result.setCode(ResultStatus.SUCCESS);
        result.setErrMsg("");
        result.setData(data);
        return result;
    }

    public static <T> Result<T> fail(String errMsg){
        Result<T> result = new Result<T>();
        result.setCode(ResultStatus.FAIL);
        result.setErrMsg(errMsg);
        result.setData(null);
        return result;
    }

    public static <T> Result<T>fail(String errMsg, T data){
        Result<T> result = new Result<T>();
        result.setCode(ResultStatus.FAIL);
        result.setErrMsg(errMsg);
        result.setData(data);
        return result;
    }
}
